<template>
	<view class="container">
		<u-navbar title='添加问题' :title-bold='true' :border-bottom='false' ></u-navbar>
		
		<view class="main">
			<view class="list " v-for="(item,index) in list" :key="index">
				<view class="item" @click="choose(item)">
					<view :class="item.radio?'chooseRadio radio':'radio'"></view>
					<span>{{item.title}}</span>
				</view>
				<u-line v-if="index<list.length-1"></u-line>
			</view>
		
		</view>
		<view class="bto">
			<view class="preview" @click="chooseAll">
				<view :class="chooseList.length==list.length?'chooseRadio radio':'radio'"></view>
				全选
			</view>
			<view class="edit click" @click="save">确认添加（{{chooseList.length}}）</view>
		</view>
		
	</view>
</template>

<script>
	import {moduleList} from '@/api/index.js'
	export default{
		data(){
			return{
				list:[],
				prevpage:{},
				chooseList:[]
			}
		},
		onLoad() {
			this.init()
			let pages=getCurrentPages()
			this.prevpage=pages[pages.length-2]
		},
		methods:{
			init(){
				const that=this
				moduleList().then(res=>{
					if(res && res.code==1){
						that.list=res.data.list;
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
				
			},
			choose(e){
				if(e.radio){
					e.radio=0
					this.chooseList.forEach((itm,idx)=>{
						if(e.id==itm.id){
							this.chooseList.splice(idx,1)
						}
					})
				}else{
					e.radio=1
					this.chooseList.push(e)
				}
				this.$forceUpdate()
			},
			chooseAll(){
				if(this.chooseList.length==this.list.length){
					this.chooseList=[] 
					this.list.forEach(ite=>{
						ite.radio=0
					})
				}else{
					this.chooseList=this.list
					this.list.forEach(ite=>{
						ite.radio=1
					})
				}
			},
			save(){
				this.chooseList.forEach(e=>{
					this.prevpage.$vm.medication(e)
				})
				uni.navigateBack()
			}

		}
	}
</script>

<style lang="less" >
	.container{
		.radio{
			border-radius: 50%;
			margin-right: 20rpx;
			padding: 8px;
			border: 1px solid #C7C7C7;
		}
		.chooseRadio{
			padding: 9px;
			background: url('../../static/choose.png');
			background-size: 100% 100%;
			background-repeat: no-repeat;
			border: unset;
		}
		
		.main{
			padding: 20rpx 0;
			min-height: 100vh;
			.list{
				background-color: #fff;
				.item{
					padding: 40rpx;
					display: flex;align-items: center;
					font-size: 18px;
				}
			}
			
		}
		
		.bto{
			position: sticky;
			bottom: 0;
			left: 0;
			background-color: #FFF;
			padding: 10rpx 20rpx;
			display: flex;
			justify-content: space-evenly;
			align-items: center;
			width: 100%;
			line-height: 60px;
			text-align: center;
			.preview{
				color: #6EC9E1;
				width: 30%;
				display: flex;align-items: center;
				justify-content: center;
			}
			.edit{
				width: 70%;
				height: 60px;
				color: #FFF;
				background-color: #6EC9E1;
				border-radius: 16rpx;
			}
		}
		
	}
</style>